#include <assert.h>
#include <irrlicht.h>
#include "demo.h"
#include "fx_hall.h"


using namespace irr;
using namespace irr::core;
using namespace irr::scene;


static ISceneManager *sSceneManager = 0;
static ISceneNode *sPallo = 0;
static ISceneNode *sAmme = 0;
static ISceneNode *sLamppu = 0;
static ISceneNode *sBkg = 0;


bool HallFx::init()
{
    if (!gIrrDevice || !gIrrDevice->getSceneManager())
        return false;

    //bool zipRes = gIrrDevice->getFileSystem()->addFileArchive("data_oldies_hallintaa.zip", false, false, io::EFAT_ZIP);
    //assert(zipRes);

    sSceneManager = gIrrDevice->getSceneManager()->createNewSceneManager();

    ISceneManager *sceneManager = sSceneManager;

    sceneManager->loadScene("data/oldies/hall.irr");

    //ISceneNode *light = sceneManager->getSceneNodeFromType(scene::ESNT_LIGHT);

    sPallo = sceneManager->getSceneNodeFromName("pallo");
	sAmme = sceneManager->getSceneNodeFromName("amme");
	sLamppu = sceneManager->getSceneNodeFromName("lamppu");
	sBkg = sceneManager->getSceneNodeFromName("bkg");

    sceneManager->addCameraSceneNode();
    vector3df cameraPosition(2, 2, 2);
    sceneManager->getActiveCamera()->setPosition(cameraPosition);
	sceneManager->getActiveCamera()->setTarget(sPallo->getPosition());

	sBkg->setVisible(false);
    //sHall->getMaterial(0).setFlag(video::EMF_LIGHTING, true);
    //assert(sHall->getType() == scene::ESNT_MESH || sHall->getType() == scene::ESNT_ANIMATED_MESH);
    //IMeshSceneNode *aivotMSN = (IMeshSceneNode *)sHall;
    //sceneManager->getMeshManipulator()->recalculateNormals(aivotMSN->getMesh(), true, true);
    //sceneManager->getMeshManipulator()->setVertexColors(sCube->getMesh(), video::SColor(0xffffeedd));
	

    mInitialized = true;
    return true;
}


void HallFx::render(double time, int timeMs)
{    
	vector3df eulerAngles((float)gTimeMs / 40.0f, (float)gTimeMs / 20.0f, (float)gTimeMs / 30.0f);
	sPallo->setRotation(eulerAngles);
	
	sSceneManager->drawAll();
}


void HallFx::deinit()
{
    sSceneManager->drop();
    mInitialized = false;
}
